﻿using GDGeek;
using MrPP.Myth;
using MrPP.Restful;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{//server
    public class BoxProcess : GDGeek.Singleton<BoxProcess>, IBridgeReceiver
    {
        public void Start()
        {
            BridgeBroadcast.Instance.addReceiver(this);
        }
        public void OnDestory()
        {

            BridgeBroadcast.Instance.removeReceiver(this);
        }
        [SerializeField]
        private BoxModel _boxModel;

        [SerializeField]
        private SearchModel _searchModel;

        public string handle => this.longName();

        public Task open(CabinetGroup.ItemData data)
        {
            Task task = null;
            if (data.target != -1)
            {
                
                task = BoxOperation.Instance.open(data.target);
               

            }
            else
            {
                task = BoxOperation.Instance.close();
            }
          
            return task;
        }
    
        private void checkRight()
        {

            List<CabinetGroup.ItemData> right = new List<CabinetGroup.ItemData>();
            if (CheckReceiver.Instance) 
            {
                foreach (var data in _boxModel.datas) {
                    if (data.type == Box.Type.Right) {
                        right.Add(data);
                    }
                }
            }

            CheckReceiver.Instance.loading(right.ToArray());
            Model.Instance.search.isOpen = 1;
        }

        private void checkLeft()
        {

            List<CabinetGroup.ItemData> left = new List<CabinetGroup.ItemData>();
            if (CheckReceiver.Instance)
            {
                foreach (var data in _boxModel.datas)
                {
                    if (data.type == Box.Type.Left)
                    {
                        left.Add(data);
                    }
                }
            }

            CheckReceiver.Instance.loading(left.ToArray());
            Model.Instance.search.isOpen = 1;


        }

        private void guide(CabinetGroup.ItemData data)
        {
            TaskManager.Run(open(data));
        }
        private Task refreshTask(int box) {
            if (box < 0 || box > CabinetGroup.Instance.data.cabinets.item.Length) {
                return new Task();
            }
            var item = CabinetGroup.Instance.data.cabinets.item[box];
            var op = Restful.RestfulManager.Instance.options(Restful.RestfulManager.Uri(UriManager.GetOrCreateInstance.getUri("api").value, "archives"), new Dictionary<string, string> { { "column", item.index.ToString() },{ "area", item.area.ToString() } });
            var task = Restful.RestfulManager.Instance.getArrayTask<ArchivesData>(op, delegate (ArchivesData[] datas) {
                if (_boxModel.datas.Count != 0) {
                    _boxModel.datas.Clear();
                }
                foreach (var data in datas) {
                    _boxModel.datas.Add(CabinetGroup.Instance.getItem(data));
                }
            });
            return task;
        }
        private void refresh()
        {
            TaskManager.Run(refreshTask(_boxModel.box));
        }

        public void broadcast(string evt, object data)
        {
            switch (evt)
            {
                case "box_left_page":
                    Debug.LogError("go:" + (int)(data) + "lPage:" + _boxModel.lPage);
                    _boxModel.lPage = _boxModel.lPage + (int)(data);
                    break;
                case "box_right_page":
                    _boxModel.rPage = _boxModel.rPage + (int)(data);
                    break;
                case "box_left_check":
                    checkLeft();
                    break;
                case "box_right_check":
                    checkRight();
                    break;
                case "box_box":

                    _searchModel.isOpen = -1;
                    if (_boxModel.box != (int)(data))
                    {
                        _boxModel.lPage = 0;
                        _boxModel.rPage = 0;
                        _boxModel.box = (int)(data);
                        _boxModel.datas.Clear();
                        _boxModel.clearLight();
                        refresh();
                    }
                    break;
                case "box_menu":
                    {
                        int index = (int)(data);
                        if (index >= 0 && index < _boxModel.datas.Count)
                        {
                       //     Debug.LogError(index);
                            _boxModel.menu = index;
                            _boxModel.lit = _boxModel.datas[index];
                            guide(_boxModel.datas[index]);
                        }
                        else
                        {
                            Debug.LogError("clear");
                            _boxModel.clearLight();

                        }

                    }

                    break;
            }
        }
    }
}